const http = require('http');
const url = require('url');
const { cors } = require('./utils/cors');
const dealGetRequest = require('./get');
const dealPostRequest = require('./post');

const { PORT, } = require('./constant');
const server = http.createServer((request, response) => {
    const path = url.parse(request.url, true);
    const { pathname, } = path;
    const { method, } = request;
    console.log("pathname + method", pathname, method);
    // 跨域处理
    cors(request, response);
    // 处理 GET 请求
    if(method === "GET") {
        dealGetRequest(pathname, request, response);
    }
    // 处理 POST 请求
    if(method === "POST") {
        dealPostRequest(pathname, request, response);
    }
});

// 监听服务端口
server.listen(PORT, (a) => {
    console.log(`Server Start at localhost/127.0.0.1:${ PORT }`);
});